package edu.kpi.pzks2.ui.simulation.gantt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import edu.kpi.pzks2.simulator.results.HistoryBean;
import edu.kpi.pzks2.simulator.results.TimeLine;

@SuppressWarnings("serial")
public class GanttTableModel extends AbstractTableModel {
    private List<TimeLine> timeLines;
    
    public GanttTableModel(final List<TimeLine> timeLines) {
        this.timeLines = timeLines;
        
        Collections.sort(timeLines, new Comparator<TimeLine>() {
            public int compare(TimeLine o1, TimeLine o2) {
                return o1.getTitle().compareTo(o2.getTitle());
            }
        });
    }

    public GanttTableModel() {
        this.timeLines = new ArrayList<TimeLine>();
    }
    
    public void setTimeLines(List<TimeLine> timeLines) {
        this.timeLines = timeLines;
        
        Collections.sort(timeLines, new Comparator<TimeLine>() {
            public int compare(TimeLine o1, TimeLine o2) {
                return o1.getTitle().compareTo(o2.getTitle());
            }
        });
    }
    
    @Override
    public String getColumnName(int column) {
        if (column == 0) return "#";
        return timeLines.get(column - 1).getTitle();
    }
    
    public int getColumnCount() {
        return timeLines.size() + 1;
    }

    public int getRowCount() {
        if (timeLines.size() == 0) return 0;
        return timeLines.get(0).getIntervals().size();
    }

    public Object getValueAt(int row, int column) {
        if (column == 0) return row;
        return timeLines.get(column - 1).getIntervals().get(row);
    }
    
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) return Integer.class;
        return HistoryBean.class;
    }
}
